package com.dong.db.shard;

import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class TxtUtils {
    public static Set<String> getTxt(String path) {
        Set<String> set = new HashSet<String>();
        try {
            BufferedReader reader = new BufferedReader(new FileReader(path));
            String line = null;
            while ((line = reader.readLine()) != null) {
                if (StringUtils.isBlank(line)) {
                    continue;
                }
                if (!set.contains(line.trim())) {
                    set.add(line.trim());
                }
            }
            reader.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return set;
    }

    public static List<String> getLineTxt(String path) {
        List<String> list = new ArrayList<String>();
        try {
            BufferedReader reader = new BufferedReader(new FileReader(path));
            String line = null;
            while ((line = reader.readLine()) != null) {
                list.add(line.trim());
            }
            reader.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return list;
    }

    public static void writeTxt(String path, boolean isAppend, List<String> strs) {
        try {
            FileWriter fileWriter = new FileWriter(path, isAppend);
            for (String str : strs) {
                fileWriter.write(str);
                fileWriter.write("\r\n");
            }
            fileWriter.flush();
            fileWriter.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}